package cn.scut.user.service.impl;

import cn.scut.user.dao.UserDao;
import cn.scut.user.pojo.User;
import cn.scut.user.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {
    UserDao userDao;

    @Override
    public User userRegisterAndLogin(String openid, String username) {
        User user = userDao.selectById(openid);
        if (user == null) {
            User temp = new User(openid, username, 0, 0);
            userDao.insert(temp);
            return temp;
        } else {
            return user;
        }
    }

    @Override
    public User userUpgrade(String openid, int experience) {
        User temp = userDao.selectById(openid);
        temp.setExperience(temp.getExperience() + experience);
        temp.updateLevel();
        userDao.updateById(temp);
        return temp;
    }

    @Autowired
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
}
